package com.huiyujiaoyu.powertime.ui.login;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huiyujiaoyu.base.BaseActivity;
import com.huiyujiaoyu.http.HttpMethods;
import com.huiyujiaoyu.powertime.MainActivity;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.LoginBean;
import com.huiyujiaoyu.powertime.model.UserInfoBean;
import com.huiyujiaoyu.util.ActivityUtil;
import com.huiyujiaoyu.util.RegexUtils;
import com.huiyujiaoyu.util.TimeDownTool;
import com.huiyujiaoyu.util.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

import static com.huiyujiaoyu.http.HttpApi.HTTP_GETVERIFICATIONCODE;
import static com.huiyujiaoyu.http.HttpApi.HTTP_LOGIN;

/**
 * Created by yujunlong on 2017/7/25.
 */

public class LoginActivity extends BaseActivity implements TimeDownTool.OnTimeDownListener {
    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @BindView(R.id.icon_close)
    ImageView iconClose;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.line_phone)
    View linePhone;
    @BindView(R.id.rl_phone)
    RelativeLayout rlPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_time)
    Button btnTime;
    @BindView(R.id.btn_send)
    Button btnSend;
    @BindView(R.id.btn_reset)
    Button btnReset;
    @BindView(R.id.ll_code)
    LinearLayout llCode;
    @BindView(R.id.line_code)
    View lineCode;
    @BindView(R.id.ll_all)
    LinearLayout llAll;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.ll_hint)
    LinearLayout llHint;
    @BindView(R.id.btn_save)
    Button btnSave;
    @BindView(R.id.linear_layout01)
    LinearLayout linearLayout01;
    @BindView(R.id.line_text1)
    View lineText1;
    @BindView(R.id.tv_text)
    TextView tvText;
    @BindView(R.id.line_text2)
    View lineText2;
    @BindView(R.id.rl_text)
    RelativeLayout rlText;
    @BindView(R.id.icon_wx)
    ImageView iconWx;
    @BindView(R.id.icon_qq)
    ImageView iconQq;
    @BindView(R.id.icon_wb)
    ImageView iconWb;
    @BindView(R.id.ll_way)
    LinearLayout llWay;

    private TimeDownTool timer;
    // 第三方登录用到的变量
    //private UMShareAPI mShareAPI;

    private String login_type;

    private String login_id;

    @Override
    protected void initListener() {
        super.initListener();
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int length = etPhone.getText().toString().trim().length();
                if (length == 11) {
                    btnReset.setVisibility(View.VISIBLE);
                    btnSend.setVisibility(View.GONE);
                    btnTime.setVisibility(View.GONE);
                } else {
                    btnSend.setEnabled(false);
                    btnReset.setVisibility(View.GONE);
                    btnSend.setVisibility(View.VISIBLE);
                    btnTime.setVisibility(View.GONE);
                }
            }
        });
    }

    @OnClick({R.id.icon_close, R.id.btn_time, R.id.btn_send, R.id.btn_reset, R.id.btn_save, R.id.icon_wx, R.id.icon_qq, R.id.icon_wb})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.icon_close:
                finish();
                break;
            case R.id.btn_time:
                break;
            case R.id.btn_send:
                sendSms();
                break;
            case R.id.btn_reset:
                btnTime.setVisibility(View.VISIBLE);
                btnReset.setVisibility(View.GONE);
                btnSend.setVisibility(View.GONE);
                timer = new TimeDownTool(60000, 1000);
                timer.setOnTimeDownListener(this);
                timer.start();
                sendSms();
                break;
            case R.id.btn_save:
                goLogin();
                break;
            case R.id.icon_wx:
                break;
            case R.id.icon_qq:
                break;
            case R.id.icon_wb:
                break;
        }
    }

    private void sendSms() {
        String mobile = etPhone.getText().toString().trim();
        if (TextUtils.isEmpty(mobile)) {
            ToastUtil.makeShortText(mContext,"请输入手机号");
            return;
        }
        if (!RegexUtils.isMobilePhoneNumber(mobile)) {
            ToastUtil.makeShortText(mContext,"请输入正确的手机号");
            return;
        }
        HttpMethods.getInstance().getVerificationCode(mContext,getComp(),this,mobile,0);
    }

    private void goLogin() {
        String phone = etPhone.getText().toString().trim();
        String code = etCode.getText().toString().trim();

        if (TextUtils.isEmpty(phone)) {
            ToastUtil.makeShortText(mContext,"请输入手机号");
            return;
        }
        if (!RegexUtils.isMobilePhoneNumber(phone)) {
            ToastUtil.makeShortText(mContext,"请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            ToastUtil.makeShortText(mContext,"请输入验证码");
            return;
        }
        HttpMethods.getInstance().login(mContext,getComp(),this,phone,code);
    }

    @Override
    public void onFinish() {
        btnTime.setVisibility(View.GONE);
        btnReset.setVisibility(View.VISIBLE);
        btnSend.setVisibility(View.GONE);
        btnReset.setText("重新获取");
        btnReset.setEnabled(true);

    }

    @Override
    public void onTick(long millisUntilFinished) {
        btnTime.setEnabled(false);
        btnTime.setText((millisUntilFinished / 1000) + "秒");
    }

    @Override
    public void onNext(Object o, int httpcode) {
        super.onNext(o, httpcode);
        switch (httpcode) {
            case HTTP_GETVERIFICATIONCODE:
                ToastUtil.makeShortText(mContext,"已发送验证码");
                break;
            case HTTP_LOGIN:
                UserInfoBean.getInstance().setAccess((LoginBean) o);
                UserInfoBean.getInstance().setLogin(true);
                //||UserInfoBean.getInstance().getGender()==-1
                if(TextUtils.isEmpty(UserInfoBean.getInstance().getNickName())){
                    ActivityUtil.startActivity(mContext, ImproveInfoActivity.class);
                }else{
                    ActivityUtil.startActivity(mContext, MainActivity.class);
                }
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
